package org.example.fastproject.web.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.fastproject.core.bean.PageParam;
import org.example.fastproject.core.bean.ViewResult;
import org.example.fastproject.web.sys.model.SysPermission;
import org.example.fastproject.web.sys.model.dto.permission.PermissionInsertDto;
import org.example.fastproject.web.sys.model.dto.permission.PermissionUpdateDto;
import org.example.fastproject.web.sys.model.vo.permission.PermissionVo;
import org.example.fastproject.web.sys.service.SysPermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author 王海舟
 * @since 2023-01-16 11:50:59
 */
@RestController
@RequestMapping("/sys/sysPermission")
@Api(tags = "系统：权限管理")
public class SysPermissionController {
    @Resource
    private SysPermissionService permissionService;

    @PostMapping("/save")
    @ApiOperation("新增权限")
    @PreAuthorize("hasAuthority('permission.save')")
    public ViewResult save(@RequestBody PermissionInsertDto dto){
        boolean success = permissionService.save(dto);
        if(success)
            return ViewResult.success("权限保存成功");
        return ViewResult.error("权限保存失败");
    }

    @DeleteMapping("/remove")
    @ApiOperation("删除权限")
    @PreAuthorize("hasAuthority('permission.remove')")
    public ViewResult remove(@RequestParam Long id){
        boolean success = permissionService.remove(id);
        if(success)
            return ViewResult.success("权限删除成功");
        return ViewResult.error("权限删除失败");
    }

    @PostMapping("/update")
    @ApiOperation("更新权限信息")
    @PreAuthorize("hasAuthority('permission.update')")
    public ViewResult update(@RequestBody PermissionUpdateDto dto){
        boolean success = permissionService.update(dto);
        if(success)
            return ViewResult.success("权限更新成功");
        return ViewResult.error("权限更新失败");
    }

    @PostMapping("/page")
    @ApiOperation("分页获取权限信息列表")
    @PreAuthorize("hasAuthority('permission.page')")
    public ViewResult<Page<PermissionVo>> page(@RequestBody PageParam param){
        Page<PermissionVo> page = permissionService.page(param);
        return ViewResult.success(page);
    }

    @GetMapping("/{id}")
    @ApiOperation("通过权限id获取权限信息")
    @PreAuthorize("hasAuthority('permission.id')")
    public ViewResult<SysPermission> getById(@PathVariable Long id){
        SysPermission syspermission = permissionService.getById(id);
        return ViewResult.success(syspermission);
    }

}
